package com.may.wind.controller;

import com.may.wind.entity.Permission;
import com.may.wind.entity.Role;
import com.may.wind.pojo.Result;
import com.may.wind.service.PermissionService;
import com.may.wind.service.RoleMenuService;
import com.may.wind.service.RolePermissionService;
import com.may.wind.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 风
 */
@RestController
@Api(tags = "角色相关API")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    RolePermissionService rolePermissionService;
    @Autowired
    RoleMenuService roleMenuService;

    /**
     * 查询所有角色信息
     *
     * @return 所有角色信息
     */
    @ApiOperation(value = "查询所有角色信息", httpMethod = "GET")
    @GetMapping("/api/role/getRoles")
    public Result getRoles() {
        List<Role> roles = roleService.getAllByPermissionsAndMenus();
        return Result.ok().data("roles", roles);
    }

    /**
     * 更新角色状态
     *
     * @param role 角色
     * @return 操作结果
     */
    @ApiOperation(value = "更新角色状态", httpMethod = "PUT")
    @ApiImplicitParam(name = "role", value = "角色", required = true)
    @PutMapping("/api/role/updateRoleStatus")
    public Result updateRoleStatus(@RequestBody Role role) {
        role = roleService.updateRoleStatus(role);
        String message = "角色" + role.getNameZh() + "状态更新成功";
        return Result.ok().data("message", message);
    }

    /**
     * 添加或修改角色信息
     *
     * @param role 角色
     * @return 操作结果
     */
    @ApiOperation(value = "添加或修改角色信息", httpMethod = "PUT")
    @ApiImplicitParam(name = "role", value = "角色", required = true)
    @PutMapping("/api/role/addOrUpdate")
    public Result addOrUpdate(@RequestBody Role role) {
        roleService.addOrUpdate(role);
        rolePermissionService.savePermissionsChanges(role.getId(), role.getPermissions());
        String message = "修改角色信息成功";
        return Result.ok().data("message", message);
    }

    /**
     * 修改角色信息
     *
     * @param role 角色
     * @return 操作结果
     */
    @ApiOperation(value = "添加或修改角色信息", httpMethod = "POST")
    @ApiImplicitParam(name = "role", value = "角色", required = true)
    @PostMapping("/api/role/editRole")
    public Result editRole(@RequestBody Role role) {
        roleService.editRole(role);
        return Result.ok().data("message", "修改角色成功");
    }

    /**
     * 获取权限信息
     *
     * @return 权限信息
     */
    @ApiOperation(value = "获取权限信息", httpMethod = "GET")
    @GetMapping("/api/role/getPermissions")
    public Result getPermissions() {
        List<Permission> permissions = permissionService.getAll();
        return Result.ok().data("permissions", permissions);
    }

    /**
     * 更新角色菜单
     *
     * @param roleId   角色ID
     * @param menusIds 菜单ID列表
     * @return 操作结果
     */
    @ApiOperation(value = "更新角色菜单", httpMethod = "PUT")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "query"), @ApiImplicitParam(name = "menusIds", value = "菜单ID列表", required = true)})
    @PutMapping("/api/role/updateRoleMenu")
    public Result updateRoleMenu(@RequestParam long roleId, @RequestBody Map<String, List<Long>> menusIds) {
        roleMenuService.updateRoleMenu(roleId, menusIds);
        return Result.ok().data("message", "更新成功");
    }
}
